/*
 * Copyright 2017-8-30 13:56:26 The JA-SIG Collaborative. All rights reserved.
 * distributed with thi file and available online at
 */
package com.lap.scp.admin.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lap.scd.commons.page.Page;
import com.lap.scd.commons.page.PageResult;
import com.lap.scd.commons.result.Resp;
import com.lap.scd.commons.result.Result;
import com.lap.scp.admin.ao.UserAO;
import com.lap.scp.admin.bean.UserBean;
import com.lap.scp.admin.domain.UserDO;
import com.lap.scp.admin.query.UserQueryDO;
import com.lap.scp.admin.utils.DictionaryUtils;
import com.lap.scp.admin.utils.UserUtils;

/**
 * <pre>
 * 管理员管理系统
 * </pre>
 * 
 * @author 系统自动生成 Exp
 * @Date 2017-8-30 13:56:26
 * @since 1.0
 */
@Controller
@RequestMapping("user")
public class UserController {
	/**
	 * 用户应用对象
	 */
	@Resource
	private UserAO userAO;

	@GetMapping(value = "add")
	public ModelAndView add() {
		ModelAndView andView = new ModelAndView("admin/userEdit");
		andView.addObject("userDO", new UserDO());
		andView.addObject("statusMap", DictionaryUtils.STATUS_MAP);
		return andView;
	}

	@PostMapping(value = "add")
	public ModelAndView add(UserDO userDO, BindingResult error) {
		ModelAndView andView = new ModelAndView();

		Resp<UserDO> resp = userAO.insertUser(userDO, error);
		if (resp.isSuccess()) {
			andView.setViewName("redirect:/user/list");
		} else {
			andView.setViewName("admin/userEdit");
			andView.addObject("error", resp.getMsg());
			andView.addObject("userDO", userDO);
			andView.addObject("statusMap", DictionaryUtils.STATUS_MAP);
		}
		return andView;
	}

	@RequestMapping(value = "delete/{userId}", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView deleteById(@PathVariable int userId, @RequestParam Map<String, Object> map,
			RedirectAttributes attributes) {
		ModelAndView andView = new ModelAndView();

		Result<Integer> resp = userAO.deleteById(userId);
		if (resp.isSuccess()) {
			andView.setViewName("redirect:/user/list");
			attributes.addAllAttributes(map);
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	@GetMapping(value = "get/{userId}")
	public ModelAndView queryById(@PathVariable int userId) {
		ModelAndView andView = new ModelAndView();
		Resp<UserDO> resp = userAO.queryById(userId);
		if (resp.isSuccess()) {
			andView.setViewName("admin/userDetail");
			andView.addObject("userDO", resp.getData());
			andView.addObject("sexMap", DictionaryUtils.SEX_MAP);
			andView.addObject("statusMap", DictionaryUtils.STATUS_MAP);
		}
		return andView;
	}

	@RequestMapping(value = "list", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView queryList(Page page, UserQueryDO queryDO) {
		ModelAndView andView = new ModelAndView();
		PageResult<UserDO> pr = userAO.queryPage(page, queryDO);
		if (pr.isSuccess()) {
			andView.setViewName("admin/userList");
			andView.addObject("queryDO", queryDO);
			andView.addObject("pr", pr);
			andView.addObject("statusMap", DictionaryUtils.STATUS_MAP);
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	@PostMapping(value = "edit")
	public ModelAndView edit(UserDO userDO, BindingResult error, @RequestParam Map<String, String> map,
			RedirectAttributes attributes) {
		ModelAndView andView = new ModelAndView();

		Resp<Integer> result = userAO.updateUser(userDO, error);
		if (result.isSuccess()) {
			attributes.addAllAttributes(map);
			andView.setViewName("redirect:/user/list");
		} else {
			andView.setViewName("admin/userEdit");
			andView.addObject("userDO", userDO);
			andView.addObject("statusMap", DictionaryUtils.STATUS_MAP);
			andView.addObject("error", result.getMsg());
		}
		return andView;
	}

	@GetMapping(value = "edit/{userId}")
	public ModelAndView edit(@PathVariable int userId) {
		ModelAndView andView = new ModelAndView();
		Resp<UserDO> resp = userAO.queryById(userId);
		if (resp.isSuccess()) {
			andView.setViewName("admin/userEdit");
			andView.addObject("userDO", resp.getData());
			andView.addObject("statusMap", DictionaryUtils.STATUS_MAP);
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	@GetMapping(value = "bind/{userId}")
	public ModelAndView queryRoleById(@PathVariable int userId) {
		ModelAndView andView = new ModelAndView();

		Result<UserBean> result = userAO.queryForBind(userId);
		if (result.isSuccess()) {
			andView.setViewName("admin/userBind");
			UserBean bean = result.getData();
			andView.addObject("userDO", bean.getUserDO());
			andView.addObject("roleIdList", bean.getRoleIdList());
			andView.addObject("roleList", bean.getRoleList());
			andView.addObject("statusMap", DictionaryUtils.STATUS_MAP);
		} else {
			andView.setViewName("commons/error");
		}
		return andView;
	}

	@PostMapping(value = "bindRole")
	public ModelAndView bindRole(int userId, String roleIdStr, @RequestParam Map<String, String> map,
			RedirectAttributes attributes) {
		ModelAndView andView = new ModelAndView();

		Result<Integer> result = userAO.roleBind(userId, roleIdStr);
		if (result.isSuccess()) {
			attributes.addAllAttributes(map);
			andView.setViewName("redirect:/user/list");
		} else {
			andView.setViewName("admin/userBind");
		}
		return andView;
	}

	@GetMapping(value = "updateStatus/{userId}/{status}")
	public ModelAndView updateStatus(@PathVariable Integer userId, @PathVariable int status,
			@RequestParam Map<String, String> map, RedirectAttributes attributes, HttpServletRequest request) {
		ModelAndView andView = new ModelAndView();

		Result<Integer> result = userAO.updateStatus(userId, status, UserUtils.get());
		if (result.isSuccess()) {
			andView.setViewName("redirect:/user/list");
			attributes.addAllAttributes(map);
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	@GetMapping(value = "updatePasswd/{userId}")
	public ModelAndView updatePasswd(@PathVariable Integer userId, String oldPd, String newPd) {
		ModelAndView andView = new ModelAndView();
		Result<Integer> result = userAO.updatePasswd(userId, oldPd, newPd, UserUtils.get());
		if (result.isSuccess()) {

		}
		return andView;
	}

	@GetMapping(value = "profile")
	public ModelAndView userProfile() {
		ModelAndView andView = new ModelAndView("admin/profile");
		int userId = UserUtils.getId();
		Resp<UserDO> resp = userAO.queryById(userId);
		andView.addObject("userDO", resp.getData());
		return andView;
	}

}
